package com.fsk.starter.common;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 时间工具类
 */
public class DateUtil {

    private DateUtil() {

    }

    /**
     * jdk1.8 引入的多线程安全的时间函数,获取某个时间在当天还剩余多少秒
     *
     * @param date 时间
     * @return 返回剩余秒数
     */
    public static int getTodaySeconds(Date date) {
        LocalDateTime midnight = LocalDateTime.ofInstant(date.toInstant(),
                ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
                .withSecond(0).withNano(0);
        LocalDateTime currentDateTime = LocalDateTime.ofInstant(date.toInstant(),
                ZoneId.systemDefault());
        long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
        return (int) seconds;
    }

}

